package fx.avaliation;

import beans.Avaliation;
import beans.Beer;
import beans.Judge;
import dao.DaoAvaliation;
import fx.Home;
import fx.judge.JudgeController;
import fx.menu.MenuController;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.InputMethodEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class AvaliationController implements Initializable {

    Avaliation avaliation;
    DaoAvaliation dao;
    Home application;
    Beer beer;
    final ToggleGroup group1 = new ToggleGroup();
    final ToggleGroup group2 = new ToggleGroup();

    public AvaliationController() {
    }
    @FXML
    private ResourceBundle resources;
    @FXML
    private URL location;
    @FXML
    private Button btnCancel;
    @FXML
    private Button btnSave;
    @FXML
    private CheckBox ckAcetaldehyde;
    @FXML
    private CheckBox ckAcidic;
    @FXML
    private CheckBox ckAlcoholic;
    @FXML
    private CheckBox ckAstringent;
    @FXML
    private CheckBox ckBottle;
    @FXML
    private CheckBox ckDiacetyl;
    @FXML
    private CheckBox ckDms;
    @FXML
    private CheckBox ckEstery;
    @FXML
    private CheckBox ckGrassy;
    @FXML
    private CheckBox ckLigth;
    @FXML
    private CheckBox ckMetallic;
    @FXML
    private CheckBox ckMusty;
    @FXML
    private CheckBox ckOxidized;
    @FXML
    private CheckBox ckPhenolic;
    @FXML
    private CheckBox ckSolvent;
    @FXML
    private CheckBox ckSulfur;
    @FXML
    private CheckBox ckVegetal;
    @FXML
    private CheckBox ckYeasty;
    @FXML
    private Label lbBeer;
    @FXML
    private Label lbName;
    @FXML
    private RadioButton rbApprentice;
    @FXML
    private RadioButton rbCertified;
    @FXML
    private RadioButton rbCicerone;
    @FXML
    private RadioButton rbGM;
    @FXML
    private RadioButton rbGrandMaster;
    @FXML
    private RadioButton rbHonorary;
    @FXML
    private RadioButton rbMaster;
    @FXML
    private RadioButton rbMasterCicerone;
    @FXML
    private RadioButton rbMead;
    @FXML
    private RadioButton rbNational;
    @FXML
    private RadioButton rbNonBjcp;
    @FXML
    private RadioButton rbOther;
    @FXML
    private RadioButton rbProfessional;
    @FXML
    private RadioButton rbProvisional;
    @FXML
    private RadioButton rbRank;
    @FXML
    private RadioButton rbRecognized;
    @FXML
    private RadioButton rbSensory;
    @FXML
    private RadioButton rbSommelie;
    @FXML
    private TextArea textAppearance;
    @FXML
    private TextArea textAroma;
    @FXML
    private TextField textCategory;
    @FXML
    private TextField textComments;
    @FXML
    private TextField textEntry;
    @FXML
    private TextArea textFlavor;
    @FXML
    private TextArea textMounthfeel;
    @FXML
    private TextField textOther;
    @FXML
    private TextArea textOverall;
    @FXML
    private TextField textScoreAppearance;
    @FXML
    private TextField textScoreAroma;
    @FXML
    private TextField textScoreFlavor;
    @FXML
    private TextField textScoreMounthfeel;
    @FXML
    private TextField textScoreOverall;
    @FXML
    private TextField textSpecialIngredients;
    @FXML
    private TextField textSubcategory;
    @FXML
    private TextField textSubcategorySpell;
    @FXML
    private TextField textSumScore;
    
    
    @FXML
    void cancel(ActionEvent event) {
        MenuController menu;
        try {
            menu = (MenuController) application.replaceSceneContent("menu/menu.fxml", Home.MENUWIDTH, Home.MENUHEIGTH, "BJCP-Scoresheet");
            menu.setMessage("sucess", "Welcome " + Home.loggedUser.getName());
            menu.setApp(application);
        } catch (Exception ex) {
            Logger.getLogger(JudgeController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @FXML
    void saveAvaliation(ActionEvent event) {
        avaliation = new Avaliation();

        avaliation.setAcetaldeHyde(ckAcetaldehyde.isSelected());
        avaliation.setAlcoholic(ckAlcoholic.isSelected());
        avaliation.setAstringent(ckAstringent.isSelected());
        avaliation.setDiacetyl(ckDiacetyl.isSelected());
        avaliation.setDms(ckDms.isSelected());
        avaliation.setEstery(ckEstery.isSelected());
        avaliation.setGrassy(ckGrassy.isSelected());
        avaliation.setLightStruck(ckLigth.isSelected());
        avaliation.setMetallic(ckMetallic.isSelected());
        avaliation.setMusty(ckMusty.isSelected());
        avaliation.setOxidized(ckOxidized.isSelected());
        avaliation.setPhenolic(ckPhenolic.isSelected());
        avaliation.setSolvent(ckSolvent.isSelected());
        avaliation.setSourAcid(ckAcidic.isSelected());
        avaliation.setSulfur(ckSulfur.isSelected());
        avaliation.setVegetal(ckVegetal.isSelected());
        avaliation.setYeasty(ckYeasty.isSelected());

        avaliation.setCategory(textCategory.getText());
        avaliation.setSubcategory(textSubcategory.getText());
        avaliation.setSubcategorySpell(textSubcategorySpell.getText());
        avaliation.setComments(textComments.getText());
        avaliation.setBootleInspection(ckBottle.isSelected());
        avaliation.setEntry(textEntry.getText());
        avaliation.setAroma(textAroma.getText());
        avaliation.setScoreAroma(Integer.parseInt(textScoreAroma.getText()));
        avaliation.setAppearence(textAppearance.getText());
        avaliation.setScoreAppearence(Integer.parseInt(textScoreAppearance.getText()));
        avaliation.setFlavor(textFlavor.getText());
        avaliation.setScoreFlavor(Integer.parseInt(textScoreFlavor.getText()));
        avaliation.setMouthfeel(textMounthfeel.getText());
        avaliation.setScoreMouthfeel(Integer.parseInt(textScoreMounthfeel.getText()));
        avaliation.setOverallImpression(textOverall.getText());
        avaliation.setScoreOverall(Integer.parseInt(textScoreOverall.getText()));
        avaliation.setSumScore(avaliation.getScoreAppearence() + avaliation.getScoreAroma() + avaliation.getScoreFlavor() + avaliation.getScoreMouthfeel() + avaliation.getScoreOverall());

        if (group1.getSelectedToggle() != null) {
//            avaliation.setBjcpKnowledge(group1.getSelectedToggle().getUserData().toString());
        }
        if (group2.getSelectedToggle() != null) {
            //          avaliation.setNonBjcpKnowledge(group2.getSelectedToggle().getUserData().toString());
        }
        avaliation.setJudge(application.loggedUser);
        avaliation.setBeer(beer);
        dao = new DaoAvaliation();
        dao.save(avaliation);

        try {
            MenuController menu = (MenuController) application.replaceSceneContent("menu/menu.fxml", Home.MENUWIDTH, Home.MENUHEIGTH, "BJCP-Scoresheet");
            menu.setMessage("error", "Evaluation performed successfully");
            menu.setApp(application);

        } catch (Exception ex) {
            Logger.getLogger(AvaliationController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @FXML
    void initialize() {
        assert btnSave != null : "fx:id=\"btnSave\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckAcetaldehyde != null : "fx:id=\"ckAcetaldehyde\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckAcidic != null : "fx:id=\"ckAcidic\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckAlcoholic != null : "fx:id=\"ckAlcoholic\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckAstringent != null : "fx:id=\"ckAstringent\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckBottle != null : "fx:id=\"ckBottle\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckDiacetyl != null : "fx:id=\"ckDiacetyl\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckDms != null : "fx:id=\"ckDms\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckEstery != null : "fx:id=\"ckEstery\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckGrassy != null : "fx:id=\"ckGrassy\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckLigth != null : "fx:id=\"ckLigth\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckMetallic != null : "fx:id=\"ckMetallic\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckMusty != null : "fx:id=\"ckMusty\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckOxidized != null : "fx:id=\"ckOxidized\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckPhenolic != null : "fx:id=\"ckPhenolic\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckSolvent != null : "fx:id=\"ckSolvent\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckSulfur != null : "fx:id=\"ckSulfur\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckVegetal != null : "fx:id=\"ckVegetal\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert ckYeasty != null : "fx:id=\"ckYeasty\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert lbBeer != null : "fx:id=\"lbBeer\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert lbName != null : "fx:id=\"lbName\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbApprentice != null : "fx:id=\"rbApprentice\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbCertified != null : "fx:id=\"rbCertified\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbCicerone != null : "fx:id=\"rbCicerone\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbGM != null : "fx:id=\"rbGM\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbGrandMaster != null : "fx:id=\"rbGrandMaster\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbHonorary != null : "fx:id=\"rbHonorary\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbMaster != null : "fx:id=\"rbMaster\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbMasterCicerone != null : "fx:id=\"rbMasterCicerone\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbMead != null : "fx:id=\"rbMead\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbNational != null : "fx:id=\"rbNational\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbNonBjcp != null : "fx:id=\"rbNonBjcp\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbOther != null : "fx:id=\"rbOther\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbProfessional != null : "fx:id=\"rbProfessional\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbProvisional != null : "fx:id=\"rbProvisional\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbRank != null : "fx:id=\"rbRank\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbRecognized != null : "fx:id=\"rbRecognized\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbSensory != null : "fx:id=\"rbSensory\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert rbSommelie != null : "fx:id=\"rbSommelie\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textAppearance != null : "fx:id=\"textAppearance\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textAroma != null : "fx:id=\"textAroma\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textCategory != null : "fx:id=\"textCategory\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textComments != null : "fx:id=\"textComments\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textEntry != null : "fx:id=\"textEntry\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textFlavor != null : "fx:id=\"textFlavor\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textMounthfeel != null : "fx:id=\"textMounthfeel\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textOther != null : "fx:id=\"textOther\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textOverall != null : "fx:id=\"textOverall\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textScoreAppearance != null : "fx:id=\"textScoreAppearance\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textScoreAroma != null : "fx:id=\"textScoreAroma\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textScoreFlavor != null : "fx:id=\"textScoreFlavor\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textScoreMounthfeel != null : "fx:id=\"textScoreMounthfeel\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textScoreOverall != null : "fx:id=\"textScoreOverall\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textSpecialIngredients != null : "fx:id=\"textSpecialIngredients\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textSubcategory != null : "fx:id=\"textSubcategory\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textSubcategorySpell != null : "fx:id=\"textSubcategorySpell\" was not injected: check your FXML file 'Avaliation.fxml'.";
        assert textSumScore != null : "fx:id=\"textSumScore\" was not injected: check your FXML file 'Avaliation.fxml'.";
        textScoreAppearance.setOnKeyReleased(scoreEventHandler);
        textScoreAroma.setOnKeyReleased(scoreEventHandler);
        textScoreFlavor.setOnKeyReleased(scoreEventHandler);
        textScoreMounthfeel.setOnKeyReleased(scoreEventHandler);
        textScoreOverall.setOnKeyReleased(scoreEventHandler);
        try {
            FileInputStream saveImage = new FileInputStream(System.getProperty("user.dir") + "\\src\\fx\\css\\images\\save.png");
            Image imageSave = new Image(saveImage);
            btnSave.setGraphic(new ImageView(imageSave));
            FileInputStream cancelImage = new FileInputStream(System.getProperty("user.dir") + "\\src\\fx\\css\\images\\cancel.png");
            Image imageCancel = new Image(cancelImage);
            btnCancel.setGraphic(new ImageView(imageCancel));
        } catch (FileNotFoundException ex) {
            System.out.println("Erro imagem");
        }
    }
    final EventHandler<KeyEvent> scoreEventHandler =
            new EventHandler<KeyEvent>() {
        public void handle(final KeyEvent keyEvent) {
            KeyCode code = keyEvent.getCode();
            TextField source = (TextField) keyEvent.getSource();
            if (code.isLetterKey()) {
                source.setText(source.getText().trim().substring(0, source.getText().trim().length() - 1));
                source.positionCaret(source.getText().trim().length());
            }
            if(source.getText().trim().length() > 2){
                source.setText(source.getText().trim().substring(0, 2));
            }
            switch (source.getId()) {
                case "textNoteAroma":
                    if (Integer.parseInt(source.getText().trim()) > 12) {
                        source.setText("12");
                    }
                    break;
                case "textNoteAppearance":
                    if (Integer.parseInt(source.getText().trim()) > 3) {
                        source.setText("3");
                    }
                    break;
                case "textNoteFlavor":
                    if (Integer.parseInt(source.getText().trim()) > 20) {
                        source.setText("20");
                    }
                    break;
                case "textNoteMounthfeel":
                    if (Integer.parseInt(source.getText().trim()) > 5) {
                        source.setText("5");
                    }
                    break;
                case "textNoteOverall":
                    if (Integer.parseInt(source.getText().trim()) > 10) {
                        source.setText("10");
                    }
                    break;
            }
            source.positionCaret(source.getText().trim().length());
            int sum = 0;
            int scoreAroma = 0;
            int scoreAppearence = 0;
            int scoreFlavor = 0;
            int scoreMounthfeel = 0;
            int scoreOverral = 0;
            if (!textScoreAroma.getText().trim().isEmpty()) {
                scoreAroma = Integer.parseInt(textScoreAroma.getText());
            }
            if (!textScoreAppearance.getText().trim().isEmpty()) {
                scoreAppearence = Integer.parseInt(textScoreAppearance.getText());
            }
            if (!textScoreFlavor.getText().trim().isEmpty()) {
                scoreFlavor = Integer.parseInt(textScoreFlavor.getText());
            }
            if (!textScoreMounthfeel.getText().trim().isEmpty()) {
                scoreMounthfeel = Integer.parseInt(textScoreMounthfeel.getText());
            }
            if (!textScoreOverall.getText().trim().isEmpty()) {
                scoreOverral = Integer.parseInt(textScoreOverall.getText());
            }
            sum += scoreAroma;
            sum += scoreAppearence;
            sum += scoreFlavor;
            sum += scoreMounthfeel;
            sum += scoreOverral;
            textSumScore.setText(Integer.toString(sum));
        }
    };

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        rbApprentice.setToggleGroup(group1);
        rbCertified.setToggleGroup(group1);
        rbNational.setToggleGroup(group1);
        rbHonorary.setToggleGroup(group1);
        rbProvisional.setToggleGroup(group1);
        rbRecognized.setToggleGroup(group1);
        rbMaster.setToggleGroup(group1);
        rbGM.setToggleGroup(group1);
        rbRank.setToggleGroup(group1);
        rbGrandMaster.setToggleGroup(group1);
        rbMead.setToggleGroup(group1);

        rbProfessional.setToggleGroup(group2);
        rbCicerone.setToggleGroup(group2);
        rbSensory.setToggleGroup(group2);
        rbSommelie.setToggleGroup(group2);
        rbMasterCicerone.setToggleGroup(group2);
        rbNonBjcp.setToggleGroup(group2);
        rbOther.setToggleGroup(group2);

        Judge judge = application.loggedUser;
        lbName.setText(judge.getName());
        initialize();
    }

    public void setApp(Home application) {
        this.application = application;
    }

    public void setBeer(Beer b) {
        this.beer = b;
        lbBeer.setText(this.beer.getName());
    }
}
